package org.example.resource;

import org.example.db.Query;
import org.example.model.Student;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * The Class Handle the JAX-RS response
 * @author Alex
 * @since 1.0
 * @see Query
 */
@Path("/Student")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class StudentResource{
	/**
	 *
	 * @param id 获取链接中的id并调用数据库交互函数
	 * @return 指定id的学生
	 */
	@GET
	@Path("/GetByID/{id}")
	public Student getByID(@PathParam("id")String id){
		return Query.getStudentByID(id);
	}
	
	/**
	 *
	 * @return All the students in the database with a list form
	 */
	@GET
	@Path("/GetAll")
	public List<Student> getAll(){
		return Query.getAllStudents();
	}
	
	/**
	 *
	 * @param id delete the student with the {@code id} given in the URL
	 * @return number of row affected in the database
	 */
	@DELETE
	@Path("/GetByID/{id}")
	@Produces(MediaType.TEXT_PLAIN)
	public int deleteStudent(@PathParam("id")String id){
		int result = Query.deleteStudent(id);
		return result;
	}
	
	@POST
	@Path("/")
	public Student addStudent(Student student){
		Student student1 = Query.addStudent(student);
		return student1;
	}
}
